package ifosup.matthias.socket;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class MyClientSocket implements Runnable
{
	private int port;
	private String myIp;
	
	public MyClientSocket(int port, String myIp) {
		this.port = port;
		this.myIp = myIp;
		Thread t = new Thread(this);
		t.start();
		try {
			Socket socket = new Socket(myIp,port);
			System.out.println("Connected");
			OutputStream out = socket.getOutputStream();	
			Scanner scn = new Scanner(System.in);
			String str = scn.nextLine();

			while (!str.equals("end"))
			{
				System.out.println("1.");
				out.write((str+"\n").getBytes());
				InputStream in = socket.getInputStream();
				int i=0;
				int j=0;
				while ((i=in.read())>0)
				{
					System.out.print((char)i);
					j++;
					System.out.print(""+j);
				}
				System.out.println("2.");
				str = scn.nextLine();
				
			}
			System.out.println("end");

		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public String getMyIp() {
		return myIp;
	}

	public void setMyIp(String myIp) {
		this.myIp = myIp;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		
	}

}
